/********************************************************************************
  DESCRIPTION OF THE FILE HERE
********************************************************************************/

#include "request.h"
#include "common.h"

#include "response.h"

/* the function called in remote mode */
static ptrResponse function(int value){
  ptrResponse res;
  value = value * 2 + 1;
  res = Response_create(value);
  debugInt("RESULT WILL BE", "value", value);
  debug("I WILL PRINTF THE RESPONSE NOW:\n\t");
#ifdef DEBUG  
  Response_printf(res);
#endif
  return res;
}

typedef struct SRequest {  
  int value;
} Request;

static void Request_init(ptrRequest request, int value){
  request->value = value;
}

ptrRequest Request_create(int arg){
  ptrRequest request = new(Request);
  Request_init(request, arg);
  return request;
}

ptrResponse Request_perform(ptrRequest request){
  ptrResponse response;
  if (request == NULL) throw_error("Request_perform", "null request");
  
  response = function(request->value);
  return response;
}

ptrRequest Request_createFromDataStream(void * stream){
  ptrRequest res = Request_create(*(int *)stream);
  return res;
}

void * Request_toDataStream(ptrRequest request){
  void * res;
  res = (void *)&request->value;
  return res;
}

void Request_printf (ptrRequest request){
  printf("Request = %d\n", request->value);
}

static void Request_deinit(ptrRequest request){
  /* do nothing */
};

void Request_destroy(ptrRequest request){
  if (request != NULL){
    Request_deinit(request);
    delete(request);
  }
}
